package com.bnz.web.listener;

import com.bnz.pojo.Product;

import javax.servlet.*;
import javax.servlet.annotation.WebListener;
import java.util.HashMap;
import java.util.Map;


@WebListener
public class MyApplicationListener implements ServletContextListener, ServletContextAttributeListener {

	//下面的两个方法是关于application对象的监控

	//将超市所有的商品放到application作用域中
	@Override
	public void contextInitialized(ServletContextEvent servletContextEvent) {
		Map<Integer, Product> productMap = new HashMap<>();
		productMap.put(1001,new Product(1001,"海飞丝洗发水",40,100));
		productMap.put(1002,new Product(1002,"恰恰瓜子",15,300));
		productMap.put(1003,new Product(1003,"双飞燕鼠标",20,150));
		productMap.put(1004,new Product(1004,"联想电脑",3500,120));
		productMap.put(1005,new Product(1005,"海尔洗衣机",1000,500));
		productMap.put(1006,new Product(1006,"格力空调",2300,400));
		productMap.put(1007,new Product(1007,"藿香正气丸",15,400));
		productMap.put(1008,new Product(1008,"医用口罩",40,2000));
		productMap.put(1009,new Product(1009,"华为手机",2500,200));
		productMap.put(1010,new Product(1010,"Mac电脑",12000,2800));

		ServletContext servletContext = servletContextEvent.getServletContext();
		servletContext.setAttribute("products",productMap);
	}

	@Override
	public void contextDestroyed(ServletContextEvent servletContextEvent) {
		System.out.println("MyApplicationListener---》contextDestroyed");
	}

	//下面的三个方法是关于application属性的监控
	@Override
	public void attributeAdded(ServletContextAttributeEvent servletContextAttributeEvent) {
		System.out.println("MyApplicationListener---》attributeAdded");
	}

	@Override
	public void attributeRemoved(ServletContextAttributeEvent servletContextAttributeEvent) {
		System.out.println("MyApplicationListener---》attributeRemoved");
	}

	@Override
	public void attributeReplaced(ServletContextAttributeEvent servletContextAttributeEvent) {
		System.out.println("MyApplicationListener---》attributeReplaced");
	}

}
